/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sle_announce_hdi.h"
#include "sle_hal_errcode.h"
#include "sle_hilog.h"
#include "sle_device_discovery.h"
#include "sle_common.h"
#include "sle_callback_adapter.h"

SleHdiErrorCode SleHdiAdapterStartAnnounce(uint8_t announceId)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    ret = (SleHdiErrorCode)sle_start_announce(announceId);
    return ret;
}

SleHdiErrorCode SleHdiAdapterStopAnnounce(uint8_t announceId)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    ret = (SleHdiErrorCode)sle_stop_announce(announceId);
    return ret;
}

SleHdiErrorCode SleHdiAdapterSetAnnounceData(uint8_t announceId, const SleAnnounceData *data)
{
    SLE_ASSERT_AND_RETURN_RET_LOG(data != NULL, SLE_HDI_ERRCODE_INVALID_PARAM, "SleHdiSetAnnounceData");
    sle_announce_data_t data_sdk = {
        .announce_data_len = data->announceLength,
        .seek_rsp_data_len = data->responceLength,
        .announce_data = data->announceData,
        .seek_rsp_data = data->responceData,
    };
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    ret = (SleHdiErrorCode)sle_set_announce_data(announceId, &data_sdk);
    return ret;
}

SleHdiErrorCode SleHdiAdapterSetAnnounceParam(uint8_t announceId, const SleAnnounceParam *param)
{
    SLE_ASSERT_AND_RETURN_RET_LOG(param != NULL, SLE_HDI_ERRCODE_INVALID_PARAM, "SleHdiSetAnnounceParam");
    sle_announce_param_t param_sdk;
    param_sdk.announce_handle = (uint8_t)param->handle,
    param_sdk.announce_mode = (uint8_t)param->mode,
    param_sdk.announce_gt_role = (uint8_t)param->role,
    param_sdk.announce_level = (uint8_t)param->level,
    param_sdk.announce_interval_min = param->annonceIntervalMin,
    param_sdk.announce_interval_max = param->annonceIntervalMax,
    param_sdk.announce_channel_map = (uint8_t)param->channelMap,
    param_sdk.announce_tx_power = (int8_t)param->txPower,
    param_sdk.conn_interval_min = (uint16_t)param->connectIntervalMin,
    param_sdk.conn_interval_max = (uint16_t)param->connectIntervalMax,
    param_sdk.conn_max_latency = (uint16_t)param->connectLatency,
    param_sdk.conn_supervision_timeout = (uint16_t)param->connectTimeout,
    param_sdk.ext_param = (void*)param->extParam,
    param_sdk.own_addr.type = (uint8_t)param->ownAddr.addrType;
    memcpy(param_sdk.own_addr.addr, param->ownAddr.addr, sizeof(param_sdk.own_addr.addr));
    param_sdk.peer_addr.type = (uint8_t)param->ownAddr.addrType;
    memcpy(param_sdk.peer_addr.addr, param->peerAddr.addr, sizeof(param_sdk.peer_addr.addr));
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    ret = (SleHdiErrorCode)sle_set_announce_param(announceId, &param_sdk);
    return ret;
}

SleHdiErrorCode SleHdiAdapterRegisterAnnounceCallbacks(SleAnnounceHdiCallbacks *announceCallbacks)
{
    SLE_ASSERT_AND_RETURN_RET_LOG(announceCallbacks != NULL, SLE_HDI_ERRCODE_INVALID_PARAM, "SleHdiRegisterAnnounceCallbacks");
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    ret = RegisterAnnounceCallbackAdapter(announceCallbacks);
    return ret;
}

SleHdiErrorCode GetSleAnnounceHdiOpts(SleAnnounceHdiOpts *announceOpts)
{
    SLE_ASSERT_AND_RETURN_RET_LOG(announceOpts != NULL, SLE_HDI_ERRCODE_INVALID_PARAM, "GetSleAnnounceHdiOpts");
    announceOpts->startAnnounce = SleHdiAdapterStartAnnounce;
    announceOpts->stopAnnounce = SleHdiAdapterStopAnnounce;
    announceOpts->setAnnounceData = SleHdiAdapterSetAnnounceData;
    announceOpts->setAnnounceParam = SleHdiAdapterSetAnnounceParam;
    announceOpts->registerAnnounceCbks = SleHdiAdapterRegisterAnnounceCallbacks;
    return SLE_HDI_ERRCODE_SUCC;
}
